package com.paireach.szwolaiyun.fragmentall;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.activity.Activity_B;
import com.paireach.szwolaiyun.activity.Activity_E;
import com.paireach.szwolaiyun.sz_common_widget.fragment.BaseFragment;

import szwolaiyun.paireach.com.szwolaizhuang.model.event.AppEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpErrorEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpSuccessEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.requestTag.RequestTag;
import szwolaiyun.paireach.com.szwolaizhuang.net.WayBillRequest;
import szwolaiyun.paireach.com.szwolaizhuang.net.json.JsonPaser;

/**
 * Dc场外排队提示
 * Created by Administrator on 2016/7/4.
 */
public class DcQueueOutSideClickButtonFragment extends BaseFragment implements View.OnClickListener {
    public static final String TAG = "DcQueueOutSideClickButtonFragment";
    private View view;
    private Button button;
    private String orderNumber;

    public static final DcQueueOutSideClickButtonFragment getInstance(String orderNumber) {
        DcQueueOutSideClickButtonFragment fragment = new DcQueueOutSideClickButtonFragment();
        Bundle bundle = new Bundle();
        bundle.putString("orderNumber", orderNumber);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            orderNumber = bundle.getString("orderNumber");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_dc_queue_outside_click_button, null);
        button = (Button) view.findViewById(R.id.go_to_discharge);
        button.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        if (view == button) {
            showProgressDialog();
            new WayBillRequest().getIfCanEnterDC(orderNumber);
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        if (event.getRequestTag() == RequestTag.GET_IF_CAN_ENTER_DC ||
                event.getRequestTag() == RequestTag.DC_ENTER) {
            dismissProgressDialog();
            showToast(event.getErrorMessage());
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        if (event.getRequestTag() == RequestTag.GET_IF_CAN_ENTER_DC) {
            if (event.getJson().equals("0")) {
                dismissProgressDialog();
                showToast("入场超时，请重新排队");
            } else {
                new WayBillRequest().dcEnterFactory(orderNumber);
            }

        } else if(event.getRequestTag() == RequestTag.DC_ENTER){
            dismissProgressDialog();
            JsonPaser.parseDCEnterInfo(event.getJson());
        }
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        if (event.getTag().equals("judge_sign_state")) {
            showToast(event.getExtraInfo());
            if (event.getCode() == 0) {
                Intent broadcastJump = new Intent();
                broadcastJump.setAction("com.paireach.szwolaiyun.ACTION_JUMP");
                Bundle bundle1 = new Bundle();
                bundle1.putString("orderCode",orderNumber);
                broadcastJump.putExtras(bundle1);
                getActivity().sendStickyBroadcast(broadcastJump);
            }
        }
    }
}
